home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_dict.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  17KB  |  583 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import unittest
  5. from test import test_support
  6. import sys
  7. import UserDict
  8. import cStringIO
  9.  
  10. class DictTest(unittest.TestCase):
  11.     
  12.     def test_constructor(self):
  13.         self.assertEqual(dict(), { })
  14.         self.assert_(dict() is not { })
  15.  
  16.     
  17.     def test_bool(self):
  18.         self.assert_(not { })
  19.         self.assert_({
  20.             1: 2 })
  21.         self.assert_(bool({ }) is False)
  22.         self.assert_(bool({
  23.             1: 2 }) is True)
  24.  
  25.     
  26.     def test_keys(self):
  27.         d = { }
  28.         self.assertEqual(d.keys(), [])
  29.         d = {
  30.             'a': 1,
  31.             'b': 2 }
  32.         k = d.keys()
  33.         self.assert_(d.has_key('a'))
  34.         self.assert_(d.has_key('b'))
  35.         self.assertRaises(TypeError, d.keys, None)
  36.  
  37.     
  38.     def test_values(self):
  39.         d = { }
  40.         self.assertEqual(d.values(), [])
  41.         d = {
  42.             1: 2 }
  43.         self.assertEqual(d.values(), [
  44.             2])
  45.         self.assertRaises(TypeError, d.values, None)
  46.  
  47.     
  48.     def test_items(self):
  49.         d = { }
  50.         self.assertEqual(d.items(), [])
  51.         d = {
  52.             1: 2 }
  53.         self.assertEqual(d.items(), [
  54.             (1, 2)])
  55.         self.assertRaises(TypeError, d.items, None)
  56.  
  57.     
  58.     def test_has_key(self):
  59.         d = { }
  60.         self.assert_(not d.has_key('a'))
  61.         d = {
  62.             'a': 1,
  63.             'b': 2 }
  64.         k = d.keys()
  65.         k.sort()
  66.         self.assertEqual(k, [
  67.             'a',
  68.             'b'])
  69.         self.assertRaises(TypeError, d.has_key)
  70.  
  71.     
  72.     def test_contains(self):
  73.         d = { }
  74.         self.assert_('a' not in d)
  75.         self.assert_('a' not in d)
  76.         d = {
  77.             'a': 1,
  78.             'b': 2 }
  79.         self.assert_('a' in d)
  80.         self.assert_('b' in d)
  81.         self.assert_('c' not in d)
  82.         self.assertRaises(TypeError, d.__contains__)
  83.  
  84.     
  85.     def test_len(self):
  86.         d = { }
  87.         self.assertEqual(len(d), 0)
  88.         d = {
  89.             'a': 1,
  90.             'b': 2 }
  91.         self.assertEqual(len(d), 2)
  92.  
  93.     
  94.     def test_getitem(self):
  95.         d = {
  96.             'a': 1,
  97.             'b': 2 }
  98.         self.assertEqual(d['a'], 1)
  99.         self.assertEqual(d['b'], 2)
  100.         d['c'] = 3
  101.         d['a'] = 4
  102.         self.assertEqual(d['c'], 3)
  103.         self.assertEqual(d['a'], 4)
  104.         del d['b']
  105.         self.assertEqual(d, {
  106.             'a': 4,
  107.             'c': 3 })
  108.         self.assertRaises(TypeError, d.__getitem__)
  109.         
  110.         class BadEq(object):
  111.             
  112.             def __eq__(self, other):
  113.                 raise Exc()
  114.  
  115.  
  116.         d = { }
  117.         d[BadEq()] = 42
  118.         self.assertRaises(KeyError, d.__getitem__, 23)
  119.         
  120.         class Exc(Exception):
  121.             pass
  122.  
  123.         
  124.         class BadHash(object):
  125.             fail = False
  126.             
  127.             def __hash__(self):
  128.                 if self.fail:
  129.                     raise Exc()
  130.                 else:
  131.                     return 42
  132.  
  133.  
  134.         x = BadHash()
  135.         d[x] = 42
  136.         x.fail = True
  137.         self.assertRaises(Exc, d.__getitem__, x)
  138.  
  139.     
  140.     def test_clear(self):
  141.         d = {
  142.             1: 1,
  143.             2: 2,
  144.             3: 3 }
  145.         d.clear()
  146.         self.assertEqual(d, { })
  147.         self.assertRaises(TypeError, d.clear, None)
  148.  
  149.     
  150.     def test_update(self):
  151.         d = { }
  152.         d.update({
  153.             1: 100 })
  154.         d.update({
  155.             2: 20 })
  156.         d.update({
  157.             1: 1,
  158.             2: 2,
  159.             3: 3 })
  160.         self.assertEqual(d, {
  161.             1: 1,
  162.             2: 2,
  163.             3: 3 })
  164.         d.update()
  165.         self.assertEqual(d, {
  166.             1: 1,
  167.             2: 2,
  168.             3: 3 })
  169.         self.assertRaises((TypeError, AttributeError), d.update, None)
  170.         
  171.         class SimpleUserDict:
  172.             
  173.             def __init__(self):
  174.                 self.d = {
  175.                     1: 1,
  176.                     2: 2,
  177.                     3: 3 }
  178.  
  179.             
  180.             def keys(self):
  181.                 return self.d.keys()
  182.  
  183.             
  184.             def __getitem__(self, i):
  185.                 return self.d[i]
  186.  
  187.  
  188.         d.clear()
  189.         d.update(SimpleUserDict())
  190.         self.assertEqual(d, {
  191.             1: 1,
  192.             2: 2,
  193.             3: 3 })
  194.         
  195.         class Exc(Exception):
  196.             pass
  197.  
  198.         d.clear()
  199.         
  200.         class FailingUserDict:
  201.             
  202.             def keys(self):
  203.                 raise Exc
  204.  
  205.  
  206.         self.assertRaises(Exc, d.update, FailingUserDict())
  207.         
  208.         class FailingUserDict:
  209.             
  210.             def keys(self):
  211.                 
  212.                 class BogonIter:
  213.                     
  214.                     def __init__(self):
  215.                         self.i = 1
  216.  
  217.                     
  218.                     def __iter__(self):
  219.                         return self
  220.  
  221.                     
  222.                     def next(self):
  223.                         if self.i:
  224.                             self.i = 0
  225.                             return 'a'
  226.                         
  227.                         raise Exc
  228.  
  229.  
  230.                 return BogonIter()
  231.  
  232.             
  233.             def __getitem__(self, key):
  234.                 return key
  235.  
  236.  
  237.         self.assertRaises(Exc, d.update, FailingUserDict())
  238.         
  239.         class FailingUserDict:
  240.             
  241.             def keys(self):
  242.                 
  243.                 class BogonIter:
  244.                     
  245.                     def __init__(self):
  246.                         self.i = ord('a')
  247.  
  248.                     
  249.                     def __iter__(self):
  250.                         return self
  251.  
  252.                     
  253.                     def next(self):
  254.                         if self.i <= ord('z'):
  255.                             rtn = chr(self.i)
  256.                             self.i += 1
  257.                             return rtn
  258.                         
  259.                         raise StopIteration
  260.  
  261.  
  262.                 return BogonIter()
  263.  
  264.             
  265.             def __getitem__(self, key):
  266.                 raise Exc
  267.  
  268.  
  269.         self.assertRaises(Exc, d.update, FailingUserDict())
  270.         
  271.         class badseq(object):
  272.             
  273.             def __iter__(self):
  274.                 return self
  275.  
  276.             
  277.             def next(self):
  278.                 raise Exc()
  279.  
  280.  
  281.         self.assertRaises(Exc, { }.update, badseq())
  282.         self.assertRaises(ValueError, { }.update, [
  283.             (1, 2, 3)])
  284.  
  285.     
  286.     def test_fromkeys(self):
  287.         self.assertEqual(dict.fromkeys('abc'), {
  288.             'a': None,
  289.             'b': None,
  290.             'c': None })
  291.         d = { }
  292.         self.assert_(d.fromkeys('abc') is not d)
  293.         self.assertEqual(d.fromkeys('abc'), {
  294.             'a': None,
  295.             'b': None,
  296.             'c': None })
  297.         self.assertEqual(d.fromkeys((4, 5), 0), {
  298.             4: 0,
  299.             5: 0 })
  300.         self.assertEqual(d.fromkeys([]), { })
  301.         
  302.         def g():
  303.             yield 1
  304.  
  305.         self.assertEqual(d.fromkeys(g()), {
  306.             1: None })
  307.         self.assertRaises(TypeError, { }.fromkeys, 3)
  308.         
  309.         class dictlike(dict):
  310.             pass
  311.  
  312.         self.assertEqual(dictlike.fromkeys('a'), {
  313.             'a': None })
  314.         self.assertEqual(dictlike().fromkeys('a'), {
  315.             'a': None })
  316.         self.assert_(type(dictlike.fromkeys('a')) is dictlike)
  317.         self.assert_(type(dictlike().fromkeys('a')) is dictlike)
  318.         
  319.         class mydict(dict):
  320.             
  321.             def __new__(cls):
  322.                 return UserDict.UserDict()
  323.  
  324.  
  325.         ud = mydict.fromkeys('ab')
  326.         self.assertEqual(ud, {
  327.             'a': None,
  328.             'b': None })
  329.         self.assert_(isinstance(ud, UserDict.UserDict))
  330.         self.assertRaises(TypeError, dict.fromkeys)
  331.         
  332.         class Exc(Exception):
  333.             pass
  334.  
  335.         
  336.         class baddict1(dict):
  337.             
  338.             def __init__(self):
  339.                 raise Exc()
  340.  
  341.  
  342.         self.assertRaises(Exc, baddict1.fromkeys, [
  343.             1])
  344.         
  345.         class BadSeq(object):
  346.             
  347.             def __iter__(self):
  348.                 return self
  349.  
  350.             
  351.             def next(self):
  352.                 raise Exc()
  353.  
  354.  
  355.         self.assertRaises(Exc, dict.fromkeys, BadSeq())
  356.         
  357.         class baddict2(dict):
  358.             
  359.             def __setitem__(self, key, value):
  360.                 raise Exc()
  361.  
  362.  
  363.         self.assertRaises(Exc, baddict2.fromkeys, [
  364.             1])
  365.  
  366.     
  367.     def test_copy(self):
  368.         d = {
  369.             1: 1,
  370.             2: 2,
  371.             3: 3 }
  372.         self.assertEqual(d.copy(), {
  373.             1: 1,
  374.             2: 2,
  375.             3: 3 })
  376.         self.assertEqual({ }.copy(), { })
  377.         self.assertRaises(TypeError, d.copy, None)
  378.  
  379.     
  380.     def test_get(self):
  381.         d = { }
  382.         self.assert_(d.get('c') is None)
  383.         self.assertEqual(d.get('c', 3), 3)
  384.         d = {
  385.             'a': 1,
  386.             'b': 2 }
  387.         self.assert_(d.get('c') is None)
  388.         self.assertEqual(d.get('c', 3), 3)
  389.         self.assertEqual(d.get('a'), 1)
  390.         self.assertEqual(d.get('a', 3), 1)
  391.         self.assertRaises(TypeError, d.get)
  392.         self.assertRaises(TypeError, d.get, None, None, None)
  393.  
  394.     
  395.     def test_setdefault(self):
  396.         d = { }
  397.         self.assert_(d.setdefault('key0') is None)
  398.         d.setdefault('key0', [])
  399.         self.assert_(d.setdefault('key0') is None)
  400.         d.setdefault('key', []).append(3)
  401.         self.assertEqual(d['key'][0], 3)
  402.         d.setdefault('key', []).append(4)
  403.         self.assertEqual(len(d['key']), 2)
  404.         self.assertRaises(TypeError, d.setdefault)
  405.         
  406.         class Exc(Exception):
  407.             pass
  408.  
  409.         
  410.         class BadHash(object):
  411.             fail = False
  412.             
  413.             def __hash__(self):
  414.                 if self.fail:
  415.                     raise Exc()
  416.                 else:
  417.                     return 42
  418.  
  419.  
  420.         x = BadHash()
  421.         d[x] = 42
  422.         x.fail = True
  423.         self.assertRaises(Exc, d.setdefault, x, [])
  424.  
  425.     
  426.     def test_popitem(self):
  427.         for copymode in (-1, 1):
  428.             for log2size in range(12):
  429.                 size = 2 ** log2size
  430.                 a = { }
  431.                 b = { }
  432.                 for i in range(size):
  433.                     a[repr(i)] = i
  434.                     if copymode < 0:
  435.                         b[repr(i)] = i
  436.                         continue
  437.                 
  438.                 if copymode > 0:
  439.                     b = a.copy()
  440.                 
  441.                 for i in range(size):
  442.                     (ka, va) = ta = a.popitem()
  443.                     self.assertEqual(va, int(ka))
  444.                     (kb, vb) = tb = b.popitem()
  445.                     self.assertEqual(vb, int(kb))
  446.                     if copymode < 0:
  447.                         pass
  448.                     self.assert_(not (ta != tb))
  449.                 
  450.                 self.assert_(not a)
  451.                 self.assert_(not b)
  452.             
  453.         
  454.         d = { }
  455.         self.assertRaises(KeyError, d.popitem)
  456.  
  457.     
  458.     def test_pop(self):
  459.         d = { }
  460.         (k, v) = ('abc', 'def')
  461.         d[k] = v
  462.         self.assertRaises(KeyError, d.pop, 'ghi')
  463.         self.assertEqual(d.pop(k), v)
  464.         self.assertEqual(len(d), 0)
  465.         self.assertRaises(KeyError, d.pop, k)
  466.         x = 0x10000000000000L
  467.         y = 0x10000000000000L
  468.         h = {
  469.             x: 'anything',
  470.             y: 'something else' }
  471.         self.assertEqual(h[x], h[y])
  472.         self.assertEqual(d.pop(k, v), v)
  473.         d[k] = v
  474.         self.assertEqual(d.pop(k, 1), v)
  475.         self.assertRaises(TypeError, d.pop)
  476.         
  477.         class Exc(Exception):
  478.             pass
  479.  
  480.         
  481.         class BadHash(object):
  482.             fail = False
  483.             
  484.             def __hash__(self):
  485.                 if self.fail:
  486.                     raise Exc()
  487.                 else:
  488.                     return 42
  489.  
  490.  
  491.         x = BadHash()
  492.         d[x] = 42
  493.         x.fail = True
  494.         self.assertRaises(Exc, d.pop, x)
  495.  
  496.     
  497.     def test_mutatingiteration(self):
  498.         d = { }
  499.         d[1] = 1
  500.         
  501.         try:
  502.             for i in d:
  503.                 d[i + 1] = 1
  504.         except RuntimeError:
  505.             pass
  506.  
  507.         self.fail("changing dict size during iteration doesn't raise Error")
  508.  
  509.     
  510.     def test_repr(self):
  511.         d = { }
  512.         self.assertEqual(repr(d), '{}')
  513.         d[1] = 2
  514.         self.assertEqual(repr(d), '{1: 2}')
  515.         d = { }
  516.         d[1] = d
  517.         self.assertEqual(repr(d), '{1: {...}}')
  518.         
  519.         class Exc(Exception):
  520.             pass
  521.  
  522.         
  523.         class BadRepr(object):
  524.             
  525.             def __repr__(self):
  526.                 raise Exc()
  527.  
  528.  
  529.         d = {
  530.             1: BadRepr() }
  531.         self.assertRaises(Exc, repr, d)
  532.  
  533.     
  534.     def test_le(self):
  535.         self.assert_(not ({ } < { }))
  536.         self.assert_(not ({
  537.             1: 2 } < {
  538.             0x1L: 0x2L }))
  539.         
  540.         class Exc(Exception):
  541.             pass
  542.  
  543.         
  544.         class BadCmp(object):
  545.             
  546.             def __eq__(self, other):
  547.                 raise Exc()
  548.  
  549.  
  550.         d1 = {
  551.             BadCmp(): 1 }
  552.         d2 = {
  553.             1: 1 }
  554.         
  555.         try:
  556.             d1 < d2
  557.         except Exc:
  558.             pass
  559.  
  560.         self.fail("< didn't raise Exc")
  561.  
  562.  
  563. import mapping_tests
  564.  
  565. class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
  566.     type2test = dict
  567.  
  568.  
  569. class Dict(dict):
  570.     pass
  571.  
  572.  
  573. class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol):
  574.     type2test = Dict
  575.  
  576.  
  577. def test_main():
  578.     test_support.run_unittest(DictTest, GeneralMappingTests, SubclassMappingTests)
  579.  
  580. if __name__ == '__main__':
  581.     test_main()
  582.  
  583.